package com.neuedu.corejava.ch10;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class ListClass {

	public static void main(String[] args) {
		//List集合
		//用于存放有序的数据，List集合的元素可能重复
		
		List list = new ArrayList();
		
		Computer computer = new Computer("惠普", 2000);
		Computer computer1 = new Computer("华为", 3000);
		Computer computer2 = new Computer("联想", 4000);
		Computer computer4 = new Computer("联想", 4000);
		
		list.add(computer);
		list.add(computer1);
		list.add(computer2);
		list.add(1,computer4);//数据插入
		list.add(computer); //list可以把同一个对象多次保存到集合中
		
		System.out.println("size  "+list.size());
		
		//查询 ---遍历查询
		Iterator i = list.iterator();
		while (i.hasNext()) {			
			Computer computer3 = (Computer) i.next();
			System.out.println(computer3);		
		}
		System.out.println("---------------------------------");
		//使用for循环迭代集合
		for (int j = 0; j < list.size(); j++) {
			Computer dcomputer = (Computer) list.get(j);
			System.out.println(dcomputer);
		}
		System.out.println("--------------------------");
		
		list.remove(1);
		list.remove(computer);
		//集合如果不去指定数据类型，则可以保存任何引用数据类型
		list.add(new String("hello"));
		list.add(new Date());
		
		//for each
		for (Object object : list) {
			//如果保存了多种数据类型，则在取数据时必须进行类型判断
			if (object instanceof String) {
				String str1 = (String) object;
			}else if (object instanceof Computer) {
				Computer computer3 = (Computer) object;
			}else if (object instanceof Date) {
				Date date = (Date) object;
			}
			System.out.println(object);
		}

	}

}
